package tools;

import com.drew.imaging.ImageMetadataReader;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifIFD0Directory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Base64;

public class ImageTools {

    public static String Image2Base64(String file) throws IOException {
        File inputFile = new File(file);  // 替换为你的图片文件路径
        BufferedImage inputImage = ImageIO.read(inputFile);

// 步骤2: 创建压缩版本的图像
        Image scaledImg = inputImage.getScaledInstance(256, 256, Image.SCALE_SMOOTH);
        BufferedImage outputImage = new BufferedImage(256, 256, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = outputImage.createGraphics();
        g2d.drawImage(scaledImg, 0, 0, null);
        g2d.dispose();


        // 步骤3: 将图像转换为Base64
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(outputImage, "png", baos);  // 指定图片格式
        byte[] imageBytes = baos.toByteArray();
        String base64Image = Base64.getEncoder().encodeToString(imageBytes);
        return base64Image;
//        return "data:image/png;base64,"+base64Image;
    }
    public static String encodeImageToBase64(String file) throws IOException {

        File imageFile = new File(file);
        BufferedImage originalImage = ImageIO.read(imageFile);
        BufferedImage orientedImage = adjustOrientation(originalImage, imageFile);
        BufferedImage resizedImage = resizeImage(orientedImage, 256, 256);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//        File writefile = new File("/Users/zgw/code/java/htprov4/out/artifacts/unnamed/uploads/3.jpg");
//        ImageIO.write(resizedImage, "jpg", writefile);
        ImageIO.write(resizedImage, "jpg", outputStream);
        byte[] imageBytes = outputStream.toByteArray();
//        return "data:image/png;base64,"+Base64.getEncoder().encodeToString(imageBytes);
        return Base64.getEncoder().encodeToString(imageBytes);
    }

    private static BufferedImage adjustOrientation(BufferedImage image, File imageFile) throws IOException {
        try {
            Metadata metadata = ImageMetadataReader.readMetadata(imageFile);
            ExifIFD0Directory directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
            if (directory != null && directory.containsTag(ExifIFD0Directory.TAG_ORIENTATION)) {
                int orientation = directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
                return rotateImageByOrientation(image, orientation);
            }
        } catch (Exception e) {
            System.out.println("Could not read EXIF information: " + e);
        }
        return image;
    }

    private static BufferedImage rotateImageByOrientation(BufferedImage image, int orientation) {
//        int width = image.getWidth();
//        int height = image.getHeight();
//        AffineTransform transform = new AffineTransform();
//
//        switch (orientation) {
//            case 1: // Normal
//                break;
//            case 6: // Rotate 90 CW
//                transform.rotate(Math.toRadians(90), width / 2.0, height / 2.0);
//                transform.translate(0, -width);
//                break;
//            case 3: // Rotate 180
//                transform.rotate(Math.toRadians(180), width / 2.0, height / 2.0);
//                transform.translate(-width, -height);
//                break;
//            case 8: // Rotate 270 CW (or 90 CCW)
//                transform.rotate(Math.toRadians(270), width / 2.0, height / 2.0);
//                transform.translate(-height, 0);
//                break;
//        }
//
//        BufferedImage newImage = new BufferedImage(orientation == 6 || orientation == 8 ? height : width,
//                orientation == 6 || orientation == 8 ? width : height,
//                image.getType());
//        Graphics2D g = newImage.createGraphics();
//        g.setTransform(transform);
//        g.drawImage(image, 0, 0, null);
//        g.dispose();
//        return newImage;


        int width = image.getWidth();
        int height = image.getHeight();
        AffineTransform transform = new AffineTransform();
        System.out.println("orientation: " + orientation);
//        orientation=8;
        switch (orientation) {
            case 1: // Normal
                return image;
            case 6: // Rotate 90 CW

                transform.translate((height - width) / 2, (height - width) / 2);
                transform.rotate(Math.toRadians(90), width / 2.0, height / 2.0);
                break;
            case 3: // Rotate 180
                transform.rotate(Math.toRadians(180), width / 2.0, height / 2.0);
                transform.translate(-width, -height);
                break;
            case 8: // Rotate 270 CW (or 90 CCW)
                transform.rotate(Math.toRadians(270), width / 2.0, height / 2.0);
                transform.translate(-height, 0);
                break;
        }

        BufferedImage newImage = new BufferedImage(orientation == 6 || orientation == 8 ? height : width,
                orientation == 1 || orientation == 3 ? width : height,
                image.getType());
//        Graphics2D g = newImage.createGraphics();
//
//        g.drawImage(image, 0, 0, null);
//        g.transform(transform);
//        g.dispose();

//        Graphics2D g = image.createGraphics();
//        g.setTransform(transform);
//        g.drawImage(image, 0, 0, null);
//        g.dispose();
        BufferedImage dest = new BufferedImage(orientation == 6 || orientation == 8 ? height : width,
                orientation == 1 || orientation == 3 ? width : height,
                image.getType());
//        BufferedImage dest = new BufferedImage(height,width,image.getType());
        Graphics2D graphics2D = dest.createGraphics();
//        graphics2D.translate((height - width) / 2, (height - width) / 2);
//        graphics2D.setTransform(new AffineTransform());
//        graphics2D.drawImage(image, 0, 0, null);
        graphics2D.setTransform(transform);
//        double theta = (Math.PI*2 ) ;
//        graphics2D.rotate(theta, (width)/2 , (height )/2 );
        graphics2D.drawRenderedImage(image, null);
        return dest;


    }

    private static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
        Image resultingImage = originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH);
        BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = outputImage.createGraphics();
        g.drawImage(resultingImage, 0, 0, null);
        g.dispose();
        return outputImage;
    }
    public static void main(String[] args) {
        try {
//            String base64 = Image2Base64("/Users/zgw/code/java/htprov4/out/artifacts/unnamed/uploads/02d53e6602ce6eaf2e0f743c.jpg");
//            System.out.println(base64);
//            System.out.println("------------------");
            String base642 = encodeImageToBase64("/Users/zgw/temp/output3.png");
            System.out.println(base642);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
